package com.imooc.smartcabinet.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;

public class MainFrame extends JFrame {
    JToolBar toolBar = null;
    JTabbedPane tabbedPane = null;
    JButton addMqttClietBtn;

    public MainFrame() {

        JPanel center = new JPanel(new BorderLayout());
        center.add(getToolBar(), BorderLayout.NORTH);
        center.add(getTabbedPane(), BorderLayout.CENTER);
        this.setFocusable(true);
        this.setName("智能柜终端模拟器");
        this.setBounds(new Rectangle(800, 700));
        this.setContentPane(center);
        this.setAlwaysOnTop(true);

    }

    public JToolBar getToolBar() {
        if (toolBar == null) {
            toolBar = new JToolBar("工具栏");
            addMqttClietBtn = new JButton("添加客户端");
            addMqttClietBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    MqttClientPanel mqttClientPanel = new MqttClientPanel();
                    int num = getTabbedPane().getTabCount() + 1;
                    getTabbedPane().add("Client_" + num, mqttClientPanel);
                }
            });
            toolBar.add(addMqttClietBtn);
            toolBar.addSeparator();
        }
        return toolBar;
    }

    public JTabbedPane getTabbedPane() {
        if (tabbedPane == null) {
            tabbedPane = new JTabbedPane();
        }
        return tabbedPane;
    }


}
